package com.ruoyi.system.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.system.domain.OutapiClientApiRel;
import com.ruoyi.system.domain.OutapiClientConfig;
import com.ruoyi.system.domain.dto.AppDTO;
import com.ruoyi.system.service.IOutapiClientApiRelService;
import com.ruoyi.system.service.IOutapiClientConfigService;
import com.ruoyi.system.service.IOutapiInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 对外接口配置实体Controller
 *
 * @author wjialong
 */
@RestController
@RequestMapping("/outapi/app")
public class OutapiClientConfigController extends BaseController {
    @Autowired
    private IOutapiClientConfigService clientConfigService;

    @Autowired
    private IOutapiClientApiRelService relService;

    @Autowired
    private IOutapiInfoService infoService;

    /**
     * 查询对外接口配置实体列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OutapiClientConfig outapiClientConfig) {
        startPage();
        List<OutapiClientConfig> list = clientConfigService.selectOutapiClientConfigList(outapiClientConfig);
        return getDataTable(list);
    }


    /**
     * 获取对外接口配置实体详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        AppDTO appDTO = new AppDTO();
        appDTO.setApp(clientConfigService.selectOutapiClientConfigById(id));
        OutapiClientApiRel rel = new OutapiClientApiRel();
        rel.setConfigId(id);
        List<OutapiClientApiRel> relList = relService.selectOutapiClientApiRelList(rel);
        List<String> list = new ArrayList<>();
        relList.forEach(r -> {
            list.add(r.getApiId());
        });
        appDTO.setApiList(list);
        return AjaxResult.success(appDTO);
    }

    /**
     * 新增对外接口配置实体
     */
    @PostMapping
    public AjaxResult add(@RequestBody AppDTO appDTO) {
        // 获取参数
        OutapiClientConfig outapiClientConfig = appDTO.getApp();
        // 执行clientConfig表的insert
        clientConfigService.insertOutapiClientConfig(outapiClientConfig);
        // 进行rel表的修改
        relService.addRels(outapiClientConfig.getId(), appDTO.getApiList());
        return AjaxResult.success();
    }

    /**
     * 修改对外接口配置实体
     */
    @PutMapping
    public AjaxResult edit(@RequestBody AppDTO appDTO) {
        // 获取参数
        OutapiClientConfig outapiClientConfig = appDTO.getApp();
        // 执行clientConfig表的update
        clientConfigService.updateOutapiClientConfig(outapiClientConfig);
        // 进行rel表的修改
        OutapiClientApiRel rel = new OutapiClientApiRel();
        rel.setConfigId(outapiClientConfig.getId());
        relService.deleteRel(rel);
        relService.addRels(outapiClientConfig.getId(), appDTO.getApiList());

        return AjaxResult.success();
    }

    /**
     * 删除对外接口配置实体
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id) {
        // 进行rel表的删除
        OutapiClientApiRel rel = new OutapiClientApiRel();
        rel.setConfigId(id);
        relService.deleteRel(rel);
        // 执行clientConfig表的删除
        clientConfigService.deleteOutapiClientConfigById(id);
        return AjaxResult.success();
    }


//    /**
//     * 导出对外接口配置实体列表
//     */
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, OutapiClientConfig outapiClientConfig) {
//        List<OutapiClientConfig> list = tDmGraphIOutapiClientConfigService.selectOutapiClientConfigList(outapiClientConfig);
//        ExcelUtil<OutapiClientConfig> util = new ExcelUtil<OutapiClientConfig>(OutapiClientConfig.class);
//        util.exportExcel(response, list, "对外接口配置实体数据");
//    }
}
